#include "common.h"

//#define USE_SUPER_SPECULAR
//#define USE_ORIGINAL_SSAO
//#define HBAO_WORLD_JITTER

uniform	sampler2D	s_half_depth;

#include "lmodel.h"
#include "hmodel.h"

#include "ssao_blur.ps"
#include "ssao.ps"
#include "ssao_hbao.ps"

layout(location = TEXCOORD0) in vec4 tc0;	// tc.xy, tc.w = tonemap scale
layout(location = TEXCOORD1) in vec2 tcJ;	// jitter coords

layout(location = COLOR0) out vec4 low;
layout(location = COLOR1) out vec4 high;

void main ()
{
	// Sample the buffers:
	float4	P = tex2D( s_position, tc0.xy );	// position.(mtl or sun)
	half4	N = tex2D( s_normal, tc0.xy);		// normal.hemi
	half4	D = tex2D( s_diffuse, tc0.xy);		// rgb.gloss
	half4	L = tex2D( s_accumulator, tc0.xy);	// diffuse.specular

#ifdef         USE_SUPER_SPECULAR
	{
		half ds = dot( D.rgb, vec3(1.f/3.f) );
		D.w = max( D.w, ds*ds/8.f );
	}
#endif

#ifdef  FORCE_GLOSS
	D.w = FORCE_GLOSS;
#endif

#ifdef         USE_GAMMA_22
	D.rgb = ( D.rgb*D.rgb ); // pow(2.2)
#endif

        // static sun
	half mtl = P.w;

#ifdef        USE_R2_STATIC_SUN
	half sun_occ = P.w*2;

	mtl = xmaterial;
	L += Ldynamic_color * sun_occ * plight_infinity	(mtl, P.xyz, N.xyz, Ldynamic_dir);
#endif

	// hemisphere
	half3 hdiffuse, hspecular;

	//  Calculate SSAO
//#ifdef USE_SSAO_BLUR
//	half occ = ssao_blur_ps(tc0.xy);
#ifdef USE_HBAO
	float occ = calc_hbao(P.z, N, tc0.xy);
#else
	half occ = calc_ssao(P.xyz, N.xyz, tc0.xy, tcJ);
#endif

        hmodel		(hdiffuse, hspecular, mtl, N.w, D.w, P.xyz, N.xyz);
//      hmodel		(hdiffuse, hspecular, mtl, 1, D.w, P.xyz, N.xyz);
//	hdiffuse*=hdiffuse;			//. high contrast hemi
//	hdiffuse*=(D.rgb*0.8 + 0.2f);	// rise texture contrast for diffuse lighting
//	hdiffuse = 0.8;
//	hdiffuse *= (occ*(D.rgb + .1f));
	hdiffuse *= occ;
	hspecular *= occ;

        half4         light       = half4         (L.rgb + hdiffuse, L.w)        ;
        half4         C           = D*light       ;                             // rgb.gloss * light(diffuse.specular)
//        half3         spec        = (C.rgb*.5f + .5f)*C.w + hspecular + hspecular + hspecular;			// replicated specular
	half3         spec        = C.www         + hspecular;      // replicated specular

#ifdef         USE_SUPER_SPECULAR
                      spec      = (C.rgb*.5f + .5f)*C.w + hspecular        ;
#endif
//		half3       color     = C.rgb + D.rgb*spec     ;
//		half3       color     = C.rgb + (D.rgb*spec+spec)/0.5f;
		half3       color     = C.rgb + spec     ;
//		half3       color     = C.rgb + D.rgb*spec+hspecular+hspecular     ;	//	More realistic and contrast specular - Ugrumiy edition

////////////////////////////////////////////////////////////////////////////////
/// For Test ///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#ifdef         DBG_TEST_NMAP
//. hemi + sun + lighting + specular
					color 	= 	hdiffuse + L.rgb + N;
#endif

#ifdef         DBG_TEST_NMAP_SPEC
//. hemi + sun + lighting + specular
					color 	= 	hdiffuse + L.rgb + N + spec;
#endif

#ifdef         DBG_TEST_LIGHT
//. hemi + sun + lighting + specular
					color 	= 	hdiffuse + L.rgb;
#endif

#ifdef         DBG_TEST_LIGHT_SPEC
//. hemi + sun + lighting + specular
					color 	= 	hdiffuse + L.rgb + spec;
#endif

#ifdef         DBG_TEST_SPEC
//. only lighting and specular
					color 		= spec;
#endif
////////////////////////////////////////////////////////////////////////////////

        // here should be distance fog
		float3        	pos        		= P.xyz;
		float         	distance		= length		(pos);
		float         	fog				= saturate		(distance*fog_params.w + fog_params.x); //
						color			= lerp     		(color,fog_color.rgb,fog);        			//
		half        	skyblend		= saturate		(fog*fog);

#ifdef         DBG_TMAPPING
        color                        	= D.xyz;
#endif

        half          	tm_scale        = tc0.w;                // interpolated from VS

#ifdef        USE_SUPER_SPECULAR
        color        	= spec          - hspecular	;
#endif
// color 		= N; //show normals
// color 		= N.w; //show normals
//   color 		= float4(occ.xxx,1.0f); //show occlusion
//	color		= float3(occ);
        tonemap        	(low, high, color, tm_scale )	;
        low 	= half4(low.rgb, skyblend);
        high 	= half4(high.rgb, skyblend);

//		low		= skyblend;
//		hight	= 0;
}
